package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.example2016.cjj.mycustomviewdemo.R;

import java.util.Random;

/**
 * Created by CJJ on 2017/2/23.
 */

public class CustomTitleView extends View {

    private static final String TAG = "CustomTitleView";

    private String mTitleText;

    private int mTitleSize;

    private int mTitleColor;

    private Paint mPaint;

    private Rect mTextBounds;

    public CustomTitleView(Context context) {
        this(context,null);
    }

    public CustomTitleView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //获取自定义的样式属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomTitleViewStyle,defStyleAttr,0);
        int n = typedArray.getIndexCount();
        for (int i=0;i<n ; i++){
            int attr = typedArray.getIndex(i);
            switch (attr){
                case R.styleable.MyCustomTitleViewStyle_titleText:
                    mTitleText= typedArray.getString(attr);
                    break;
                case R.styleable.MyCustomTitleViewStyle_titleTextColor:
                    mTitleColor= typedArray.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.MyCustomTitleViewStyle_titleSize:
                    //第二个参数是，根据当前屏幕分辨率计算出相应的值,默认设置为16sp
                    mTitleSize= typedArray.getDimensionPixelSize(attr,
                            (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics()));
                    break;

            }
        }
        typedArray.recycle();//回收


        mPaint= new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); //抗锯齿
        mPaint.setTextSize(mTitleSize);

        mTextBounds = new Rect();
        mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mTextBounds);
        
        //给CustomTitleView设置点击事件
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mTitleText = getRandomText();

                //重新绘制
                postInvalidate();
            }
        });

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int width=0;


        switch (widthMode){
            case MeasureSpec.EXACTLY:
                //设置了精确值或者是MATCH_PARENT

                width = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                //标识子布局限制在一定范围内，一般为wrap_content

                int desired = getPaddingLeft() + mTextBounds.width() + getPaddingRight();
                width = desired;
                break;
//            case MeasureSpec.UNSPECIFIED:
//                //表示子布局想要多大就多大，很少使用
//                break;
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int height=0;

        switch (heightMode){
            case MeasureSpec.EXACTLY:
                height = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                int desired = getPaddingTop() + mTextBounds.height() + getPaddingBottom();
                height = desired;
                break;
        }

        setMeasuredDimension(width,height);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(Color.GREEN);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint); //画矩形

        mPaint.setColor(mTitleColor);
        canvas.drawText(mTitleText,getMeasuredWidth()/2-mTextBounds.width()/2,getMeasuredHeight()/2+mTextBounds.height()/2,mPaint); //画文字

    }


    //获取一个4位的随机数
    private String getRandomText(){

        Random random = new Random();

        StringBuilder sb = new StringBuilder();

        for(int i=1; i<=4; i++){
            int randomval = random.nextInt(10);
            sb.append(randomval);
        }

        return sb.toString();
    }


}
